<?php
declare (strict_types = 1);

namespace app\common\controller;

use app\admin\controller\AdminController;
use app\admin\service\ConfigService;
use app\common\service\EmailService;
use app\Request;
use think\facade\View;

/**
 * 邮箱管理
 *
 * @Author tanruizheng
 * @DateTime 2021-07-19
 */
class Email extends AdminController
{

    /**
     * 邮箱配置
     *
     * @Author tanruizheng
     * @DateTime 2021-07-19
     * @return void
     */
    public function setting(Request $request){
        if($request->isPost()){
            $data = [
                'mail_type' => $request->param('mail_type'),//邮件发送模式
                'mail_server' => $request->param('mail_server'),//邮件服务器
                'mail_port' => $request->param('mail_port'),//邮件发送端口
                'mail_from' => $request->param('mail_from'),//发件人地址
                'mail_auth' => $request->param('mail_auth'),//密码验证
                'mail_user' => $request->param('mail_user'),//邮箱用户名
                'mail_password' => $request->param('mail_password'),//邮箱密码
                'mail_fname' => $request->param('mail_fname'),//发件人名称
            ];
            $res= ConfigService::updateConfig($data);
            return json($res);
        }
        $fields = [
            'mail_type',
            'mail_server',
            'mail_port',
            'mail_from',
            'mail_auth',
            'mail_user',
            'mail_password',
            'mail_fname',
        ];
        $config = ConfigService::getConfig($fields)['data'];
        return View::fetch('setting',[
            'config' => $config
        ]);
    }

    /**
     * 发送邮件
     *
     * @Author tanruizheng
     * @DateTime 2021-07-19
     * @return void
     */
    public function sendEmail(Request $request){
        // phpinfo();
        if($request->isPost()){
            $toEmail = $request->param('to_email');
            $subject = $request->param('subject');
            $content = $request->param('content');
            $res= EmailService::sendEmail($toEmail,$subject,$content);
            return json($res);
        }
        return View::fetch('sendEmail');
    }

    /**
     * 邮件发送日志
     *
     * @Author tanruizheng
     * @DateTime 2021-07-23
     * @param Request $request
     * @return void
     */
    public function sendLog(Request $request){
        if($request->param('_action') == 'getsendLogList'){
            $params = $request->param();
            $page = $params['page'] ?: 1;//页数
            $limit = $params['limit'] ?: 15;//条数
            $where = [];
            $result = EmailService::getSendLogList($where,intval($page),intval($limit));
            return json($result);
        }
        return View::fetch('sendLog',[
            
        ]);
    }
}
